import { getArtilesByIdAPI, getDetailByIdAPI } from '@/api/articles';
import { AppDispatch } from '..';
export const getArtilesByIdAction = (data: { channel_id?: number; timestamp?: number }) => {
  return async (dispatch: AppDispatch) => {
    const res = await getArtilesByIdAPI(data);
    dispatch({ type: 'article/saveList', payload: { ...res.data, channel_id: data.channel_id } });
  };
};

// 新建一个更新的action，不去复用保存的aciton
export const updateArtilesByIdAction = (data: { channel_id?: number; timestamp?: number }) => {
  return async (dispatch: AppDispatch) => {
    const res = await getArtilesByIdAPI(data);
    dispatch({
      type: 'article/updateListById',
      payload: { ...res.data, channel_id: data.channel_id },
    });
  };
};

export const getDetailByIdAction = (id: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await getDetailByIdAPI(id);
    // console.log('res  ----->  ', res);
    dispatch({ type: 'article/saveDetail', payload: res.data });
  };
};

//    2. 封装Action函数
export const clearArticleAction = () => {
  return {
    type: 'article/saveDetail',
    payload: {},
  };
};
